{layout name="layout/layout" /}
<style>
</style>
<div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief">
    <ul class="layui-tab-title">
        <li class="layui-this">客户列表</li>
    </ul>
</div>
<button class="layui-btn layui-btn-normal" onclick="update_user();" style="float:right">设为同一用户</button>
<a class='layui-btn layui-btn-normal' href="{:Url('User/excel')}" style="float: right;margin-right: 5px"> 导出用户</a>
<form>
    <select name="condition_type" id="search" class="selected">
        <option {if($condition[ "condition_type"]=='' )}selected{/if} value="">全部</option>
        <option {if($condition[ "condition_type"]=='user_name' )}selected{/if} value="user_name">姓名</option>
        <option {if($condition[ "condition_type"]=='nickname' )}selected{/if} value="nickname">昵称</option>
        <option {if($condition[ "condition_type"]=='user_sex' )}selected{/if} value="user_sex">性别</option>
        <option {if($condition[ "condition_type"]=='companyname' )}selected{/if} value="companyname">公司</option>
        <option {if($condition[ "condition_type"]=='user_tel' )}selected{/if} value="user_tel">手机号</option>
        <option {if($condition[ "condition_type"]=='subscribe' )}selected{/if} value="subscribe">是否关注</option>
        <option {if($condition[ "condition_type"]=='time' )}selected{/if} value="time">注册时间</option>
        <option {if($condition[ "condition_type"]=='subscribe_time' )}selected{/if} value="subscribe_time">关注时间</option>
    </select>
    <div class="layui-inline" style="margin-top: -5px;">
        <input style="display: none" class=" ee layui-input text" type="text" name="condition_val" value="{if($condition)}{$condition['condition_val']}{/if}">
        <select style="display: none" class="ee  subscribe selected" name="subscribe">
            <option {if($condition || $condition[ "subscribe"]=='1' )}selected{/if} value="1">已关注</option>
            <option {if($condition || $condition[ "subscribe"]=='0' )}selected{/if} value="0">未关注</option>
        </select>
        <div style="display: none" class="ee time">
            <div class="layui-input-inline">
                <input class="layui-input" placeholder="起始时间" type="date" name="start_time" value="{if($condition)}{$condition['start_time']}{/if}">
            </div>
            <div class="layui-input-inline">
                <input class="layui-input" placeholder="终止时间" min="" type="date" name="end_time" value="{if($condition)}{$condition['end_time']}{/if}">
            </div>
        </div>
        <div style="display: none" class="ee subscribe_time">
            <div class="layui-input-inline">
                <input class="layui-input" placeholder="起始时间" type="date" name="start_subscribe" value="{if($condition)}{$condition['start_subscribe']}{/if}">
            </div>
            <div class="layui-input-inline">
                <input class="layui-input" placeholder="终止时间" type="date" name="end_subscribe" value="{if($condition)}{$condition['end_subscribe']}{/if}">
            </div>
        </div>
    </div>
    <div class="layui-inline" style="margin-top: -5px;">
        <button class="layui-btn" data-type="reload">查询</button>
    </div>
</form>


<div class="layui-form">
    <table class="layui-table">
        <colgroup>
            <col width="2%">
            <col width="8%">
            <col width="5%">
            <col width="3%">
            <col width="5%">
            <col width="9%">
            <col width="6%">
            <col width="5%">
            <col width="5%">
            <col width="5%">
            <col width="6%">
            <col width="5%">
            <col width="8%">
            <col width="8%">
            <col width="19%">
        </colgroup>
        <thead>
            <tr>
                <th>
                    <input class="all-check" type="checkbox" style="display:block">
                </th>
                <th>昵称</th>
                <th>头像</th>
                <th>性别</th>
                <th>手机</th>
                <th>公司</th>
                <th>职位</th>
                <th>是否通过</th>
                <th>是否关注</th>
                <th>会员积分</th>
                <th>会员等级</th>
                <th>推荐人</th>
                <th>关注时间</th>
                <th>注册时间</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            {foreach name="items" item="item"}
            <tr>
                <td>
                    <input class="checkbox" type="checkbox" value="{$item.user_id}" style="display:block">
                </td>
                <td style="cursor:pointer" class="openid" openid="{$item.openid}">{$item.user_name} {if($item.nick_name)}
                    <span style="color: red">({$item.nick_name})</span> &nbsp; {/if}
                </td>
                <td>
                    {if($item.headimgurl)}
                    <img src="{$item.headimgurl}" width="40px" height="40px"> {/if}
                </td>
                <td>
                    {if condition = "($item.user_sex==1)"} 男 {elseif condition="($item.user_sex==2)"/}女{else /}未知{/if}
                </td>
                <td>{$item.user_tel}</td>
                <td>
                    {$item.company}
                    <br/> {if($item.companyname)}
                    <span style="color: red">({$item.companyname})</span>
                    {/if} &nbsp;
                </td>
                <td>
                    {$item.position} &nbsp;
                </td>
                <td>
                    {if condition="$item.is_pass == 1"}
                    <span style="color: red">通过</span> {else /}不通过{/if} &nbsp;
                </td>
                <td>
                    {if condition="$item.subscribe == 1"}
                    <span style="color: red">已关注</span> {else /}未关注{/if} &nbsp;
                </td>
                <td>
                    {$item.jifen}
                </td>

                <td>
                    {$item.vip}
                </td>
                <td>
                    {$item.referee}
                </td>
                <td>
                    {if($item.subscribe_time)} {$item.subscribe_time|date="Y-m-d H:i:s",###} {/if}
                </td>
                <td style="font-size:14px">
                    {if($item.time)} {$item.time|date="Y-m-d H:i:s",###} {/if}
                </td>
                <td>
                    <div class="layui-btn-group">
                        <a class="layui-btn layui-btn-xs bangding" data="{$item['user_id']}" href="javascript:void (0)">绑定用户</a>
                        <a class="layui-btn layui-btn-xs" href="{:Url('user/edit',['user_id' => $item['user_id']])}">编辑</a>
                        <a class="layui-btn layui-btn-danger layui-btn-xs del1" href="#" userid="{$item.user_id}">删除用户</a>
                    </div>

                    <a class="layui-btn layui-btn-normal layui-btn-xs is_test" state="{$item.is_test}" href="#" userid="{$item.user_id}">
                        {if($item.is_test == 1)}
                            取消设为同一用户
                        {else /}
                        设为同一用户
                        {/if}
                    </a>
                    {if condition="$item.is_pass == 0"}
                    <a class="user_id layui-btn layui-btn-primary layui-btn-xs" user_id="{$item['user_id']}" href="#">发送</a>
                    {/if}
                    
                </td>
            </tr>
            {/foreach}
        </tbody>
    </table>
    共{$count}条 {$page}
</div>
<script>
    $('.bangding').click(function(){
        var user_id = $(this).attr('data');
        //查询有没有指定用户
//        $.post("{:Url('user/get_zhiding')}",{user_id:user_id},function(data){
//            console.log(data);
//        },'json');
        var bangding_name = '';
        var bangding_nickname = '';

        $.ajax({
            url: "{:Url('user/get_zhiding')}",
            data: {user_id:user_id},
            type: 'post',
            success: function (data) {
                var str = '';
                if(data.code == 0){
                    str = "<span>绑定用户："+data.data.name+"</span><br/><br/>";
                }
                str += '<input class="layui-input" id="openid" type="text" name="openid" placeholder="请输入openid"><br/><br/>';
                str += '<input class="layui-btn" user_id="'+user_id+'" id="but" type="button" value="绑定">';
                layer.open({
                    title:'绑定用户',
                    type: 1,
                    skin: 'layui-layer-rim', //加上边框
                    area: ['420px', '240px'], //宽高
                    content: str
                });
                $('#but').click(function(){
                    var openid = $('#openid').val();
                    var user_id = $(this).attr('user_id');
                    $.post("{:Url('user/bangding')}",{openid:openid,user_id:user_id},function(data){
                        if(data.code != 0){
                            layer.alert(data.msg);
                            return;
                        }else {
                            layer.alert(data.msg);
							  layer.close();
                        }
                    },'json');
                });

            }
        });

    });




    $('.openid').click(function(){
       var openid = $(this).attr('openid');
        layer.alert(openid);
    });

    var type = $('#search').val();
    condition(type);
    $('#search').change(function () {
        var type = $(this).val();
        condition(type);
    });
    function condition(type) {
        $('.ee').css('display', 'none');
        if (type == '') {
            $('.ee').css('display', 'none');
        } else if (type == 'user_name' || type == 'user_sex' || type == 'companyname' || type == 'user_tel' || type=='nickname') {
            $('.text').css('display', 'block');

        } else if (type == 'subscribe') {
            $('.subscribe').css('display', 'block');
        } else if (type == 'time') {
            $('.time').css('display', 'block');

        } else if (type == 'subscribe_time') {
            $('.subscribe_time').css('display', 'block');
        }
    }
    //时间大小对比 start
    var a = "", b = "", c = "", d = "";
    $('input[name="start_time"]').bind('input propertychange', function () {
        a = $(this).val();
    });
    $('input[name="end_time"]').bind('input propertychange', function () {
        b = $(this).val();
        data(a, b);
    });
    $('input[name="start_subscribe"]').bind('input propertychange', function () {
        c = $(this).val();
    });
    $('input[name="end_subscribe"]').bind('input propertychange', function () {
        d = $(this).val();
        data(c, d);
    });
    function data(start, end) {
        var beginDate = start.replace(/\-/g, "\/");
        var endDate = end.replace(/\-/g, "\/");
        if (beginDate != "" && endDate != "" && beginDate >= endDate) {
            layer.alert("开始时间不能大于结束时间！");
            end = "";
            return false;
        }
    }
    //时间大小对比 end
    //复选框 start
    var account = $(".checkbox");

    // console.log(account);
    $(".all-check").click(function () {
        if (this.checked) {
            $(".checkbox").prop("checked", true);
            check();
        } else {
            $(".checkbox").prop("checked", false);
            check();
        }
    });
    //  ids 所有id值
    var ids= [];
    $('.checkbox').click(function () {
        check();
    });
    //查找选中的复选框
    function check() {
        ids = [];
        account.each(function () {
            if (this.checked) {
                ids.push( $(this).val());
            }

        })
    }
    function update_user(){
        if(ids.length<=0){
        layer.alert('当前没有选中');
            return;
        }
        $.ajax({
            url: '/admin/user/same_account',
            data: {ids:JSON.stringify(ids)},
            type: 'post',
            success: function (data) {
                location.reload();
            }
        });
    }
    //改变同一用户状态
    $('.is_test').click(function () {
        var id = $(this).attr('userid');
        var state = $(this).attr('state');
        is_test(id,state)
    });
    //改变同一用户状态
    function is_test(id,state){
        var new_state;
        if(state == 1){
            new_state = 0;
        }else {
            new_state = 1;
        }
        $.ajax({
            url: '/admin/user/is_test',
            data: {id:id,new_state:new_state},
            type: 'post',
            success: function (data) {
                location.reload();
            }
        });

    }


   
    //复选框 end
    $('.del1').click(function () {
        var id = $(this).attr('userid');
        layer.confirm('是否确认删除？', {
            btn: ['确认', '取消'] //按钮
        }, function () {
            $.post("{:Url('admin/user/del')}", { userid: id }, function (data) {
                window.location.reload();
                if (!data) {
                    layer.msg('删除失败');
                }
            });
        })
    });

    $('#chaxun').click(function () {
        var data = $('#cx').val();

        if (data == '') {
            layer.alert('请输入手机号或姓名');
        } else {
            $.post("{:Url('admin/user/chaxun')}", { data: data }, function (data) {
                if (data == '没有找到该用户') {
                    alert(data);
                } else {
                    /*alert(data);*/
                    var e = data;


                    window.location.href = "{:Url('/admin/user/f/id/" + e + "')}";
                }
            });
        }
    });


    //发送的ajax
    $(function () {
        $('.user_id').click(function () {
            var user_id = $(this).attr('user_id');

            $.post("{:Url('admin/user/send_user')}", { user_id: user_id }, function (data) {
//                console.log(data);
                if (data == 1) {
                    alert('保存成功！');
                } else {
                    alert(data);
                }
                window.location.reload();
            }, 'json');
        });
    })


</script>
